//
//  EditMyInfoViewController.m
//  ZhouProject
//
//  Created by Dlrj on 14-12-15.
//  Copyright (c) 2014年 admin. All rights reserved.
//

#import "EditMyInfoViewController.h"
#import "LoadingHelper.h"
#import "NetWorkingHelper.h"
#import "XiaLa.h"
#import "XiuGaiXinXi.h"

@implementation EditMyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"编辑";
    
    self.navigationItem.hidesBackButton = YES;
    
    _row = 0;
    UILabel * labPhone = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(100), kW(80), kH(25))];
    labPhone.text = @"手机号:";
    labPhone.textColor = [UIColor blackColor];
    [self.view addSubview:labPhone];
    
    UILabel * labType = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(150), kW(80), kH(25))];
    labType.text = @"类别:";
    labType.textColor = [UIColor blackColor];
    [self.view addSubview:labType];
    
    UILabel * lab4s = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(200), kW(80), kH(25))];
    lab4s.text = @"4S店:";
    lab4s.textColor = [UIColor blackColor];
    [self.view addSubview:lab4s];
    
    UILabel * labZone = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(250), kW(80), kH(25))];
    labZone.text = @"区域:";
    labZone.textColor = [UIColor blackColor];
    [self.view addSubview:labZone];
    
    _txtPhone = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(95), kW(200), kH(33.5))];
    _txtPhone.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPhone.returnKeyType = UIReturnKeyDone;
    _txtPhone.delegate = self;
    _txtPhone.text = self.strloginName;
    _txtPhone.placeholder = @"有效的手机号";
    UILabel *leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPhone setLeftView:leftView];
    [_txtPhone setLeftViewMode:UITextFieldViewModeAlways];
    _txtPhone.layer.cornerRadius = 5.0;
    //边框宽度
    _txtPhone.layer.borderWidth = 1.0f;
    //边框颜色
    _txtPhone.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;
    //内容不超出边框
    _txtPhone.clipsToBounds = YES;
    _txtPhone.userInteractionEnabled = NO;
    [self.view addSubview:_txtPhone];
    
    _txtType = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(145), kW(200), kH(33.5))];
    _txtType.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtType.returnKeyType = UIReturnKeyDone;
    _txtType.delegate = self;
    _txtType.placeholder = @"类别";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtType setLeftView:leftView];
    [_txtType setLeftViewMode:UITextFieldViewModeAlways];
    _txtType.layer.cornerRadius = 5.0;
    //边框宽度
    _txtType.layer.borderWidth = 1.0f;
    //边框颜色
    _txtType.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;
    //内容不超出边框
    _txtType.clipsToBounds = YES;
    [self.view addSubview:_txtType];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.showsSelectionIndicator=YES;
    _txtType.inputView = _pickerView;
    
    _txt4S = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(195), kW(200), kH(33.5))];
    _txt4S.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txt4S.returnKeyType = UIReturnKeyDone;
    _txt4S.delegate = self;
    _txt4S.placeholder = @"4S店";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txt4S setLeftView:leftView];
    [_txt4S setLeftViewMode:UITextFieldViewModeAlways];
    _txt4S.text = self.strshopName;
    _txt4S.clearButtonMode = UITextFieldViewModeAlways;
    _txt4S.layer.cornerRadius = 5.0;
    //边框宽度
    _txt4S.layer.borderWidth = 1.0f;
    //边框颜色
    _txt4S.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;
    //内容不超出边框
    _txt4S.clipsToBounds = YES;
    [self.view addSubview:_txt4S];
    
    _txtZone = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(245), kW(200), kH(33.5))];
    _txtZone.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtZone.returnKeyType = UIReturnKeyDone;
    _txtZone.delegate = self;
    _txtZone.placeholder = @"区域";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtZone setLeftView:leftView];
    [_txtZone setLeftViewMode:UITextFieldViewModeAlways];
    _txtZone.text = self.strshopArea;
    _txtZone.clearButtonMode = UITextFieldViewModeAlways;
    _txtZone.layer.cornerRadius = 5.0;
    //边框宽度
    _txtZone.layer.borderWidth = 1.0f;
    //边框颜色
    _txtZone.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;
    //内容不超出边框
    _txtZone.clipsToBounds = YES;
    [self.view addSubview:_txtZone];
    
    UIButton * btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLogin.frame = CGRectMake(kW(40), kH(350), kW(100), kH(35));
    [btnLogin setBackgroundImage:[UIImage imageNamed:@"hb4"] forState:UIControlStateNormal];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnLogin setTitle:@"确定" forState:UIControlStateNormal];
    //设置圆角
    btnLogin.layer.cornerRadius = 5.0;
    //内容不超出边框
    btnLogin.clipsToBounds = YES;
    [btnLogin addTarget:self  action:@selector(clickqueding) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin];
    
    UIButton * btnReg = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnReg.frame = CGRectMake(kW(180), kH(350), kW(100), kH(35));
    [btnReg setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReg setBackgroundImage:[UIImage imageNamed:@"hb1"] forState:UIControlStateNormal];
    [btnReg setTitle:@"取消" forState:UIControlStateNormal];
    //设置圆角
    btnReg.layer.cornerRadius = 5.0;
    //内容不超出边框
    btnReg.clipsToBounds = YES;
    [btnReg addTarget:self  action:@selector(clickquxiao) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnReg];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [LoadingHelper showLoadingWithView:self.view];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //获取类别列表
        _pickerData = [NetWorkingHelper XiaLa];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [LoadingHelper hiddonLoadingWithView:self.view];
            
            //循环附值对应的类别
            for (int i = 0; i < _pickerData.count; i++) {
                
                XiaLa * model = [_pickerData objectAtIndex:i];
                if ([model.strcategoryId isEqualToString:self.strcategoryId]) {
                    _txtType.text = model.strcategoryName;
                }
            }
            
            UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
//            topView.backgroundColor = [UIColor lightGrayColor];
            [topView setBarStyle:UIBarStyleDefault];
            
            UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
            
            UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
            
            NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
            
            [topView setItems:buttonsArray];
            [_txtType setInputAccessoryView:topView];
        });
    });
}

//选择类别－确定按钮点击事件
-(void)dismissKeyBoard {
    
    [_txtType resignFirstResponder];
}

//确定按钮点击事件
-(void)clickqueding
{
    [LoadingHelper showLoadingWithView:self.view];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
      NSString * str = [NetWorkingHelper XiuGaiGeRenXinXiWithcustomerId:self.struserId categoryId:self.strcategoryId shopName:_txt4S.text shopArea:_txtZone.text];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [LoadingHelper hiddonLoadingWithView:self.view];
            
            //修改成功
            if ([str isEqualToString:@"0"]) {
                XiuGaiXinXi * model = [[XiuGaiXinXi alloc]init];
                model.strType = _txtType.text;
                model.str4s = _txt4S.text;
                model.strZone = _txtZone.text;
                model.strTypeid = self.strcategoryId;
                NSDictionary * userInfo = [NSDictionary dictionaryWithObject:model forKey:@"return"];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateInfoSuccess" object:nil userInfo:userInfo];
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改信息成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                alert.tag = 100;
                [alert show];
            }
            //修改失败
            else {
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改信息失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }
        });
        
    });
}

//提示框代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //修改信息成功－确定
    if (alertView.tag == 100) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    //请求用户类别失败－确定
    else if (alertView.tag == 11) {
        [self dismissKeyBoard];
    }
}

//取消按钮点击方法
-(void)clickquxiao {
    [self.navigationController popViewControllerAnimated:YES];
}

//监听点击事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_txtPhone resignFirstResponder];
    [_txtType resignFirstResponder];
    [_txt4S resignFirstResponder];
    [_txtZone resignFirstResponder];
}

//done按键点击方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [_txtPhone resignFirstResponder];
    [_txtType resignFirstResponder];
    [_txt4S resignFirstResponder];
    [_txtZone resignFirstResponder];
    return YES;
}

//
-(BOOL)textFieldShouldClear:(UITextField *)textField {
    return YES;
}

//
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    //iPhone5适配
    if (kScreenHeight!= 480) {
        //
        if (textField == _txt4S) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  kH(40));
            } completion:nil];
        }
        else if(textField == _txtZone) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  kH(80));
            } completion:nil];
        }
    }
    //
    else {
        //
        if (textField == _txt4S) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  50);
            } completion:nil];
        }
        else if(textField == _txtZone) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  100);
            } completion:nil];
        }
    }
    if (textField == _txtType) {
        //类别数组请求成功
        if (_pickerData.count > 0) {
            _row = 0;
            for (XiaLa *model in _pickerData) {
                if ([model.strcategoryId isEqualToString:self.strcategoryId]) {
                    break;
                }
                _row++;
            }
            _row = _row<_pickerData.count?_row:0;
            XiaLa * model = [_pickerData objectAtIndex:_row];
            [_pickerView selectRow:_row inComponent:0 animated:NO];
            _txtType.text = model.strcategoryName;
            self.strcategoryId = model.strcategoryId;
            return;
        }
        //请求类别数组
        [LoadingHelper showLoadingWithView:self.view];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            _pickerData = [NetWorkingHelper XiaLa];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (_pickerData.count > 0) {
                    XiaLa * model = [_pickerData objectAtIndex:_row];
                    _txtType.text = model.strcategoryName;
                    self.strcategoryId = model.strcategoryId;
                    [_pickerView reloadAllComponents];
                    [_txtType reloadInputViews];
                }
                else {
                    _txtType.text = @"";
                    [_pickerView reloadAllComponents];
                    [_txtType reloadInputViews];
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请求用户类别失败，请检查网络后重试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    alert.tag = 11;
                    [alert show];
                }
                
                [LoadingHelper hiddonLoadingWithView:self.view];
            });
        });
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    //
    if (textField == _txt4S) {
        [UIView animateWithDuration:0.25 animations:^{
            self.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
        } completion:nil];
    }
    else if(textField == _txtZone) {
        [UIView animateWithDuration:0.25 animations:^{
            self.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
        } completion:nil];
    }
}


-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    XiaLa * model = [_pickerData objectAtIndex:row];
    return model.strcategoryName;
    
}

//有多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return _pickerData.count;
    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    XiaLa * model = [_pickerData objectAtIndex:row];
    _txtType.text = model.strcategoryName;
    self.strcategoryId = model.strcategoryId;
    _row = row;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
